import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/account/api/accountApi.dart';
import 'package:honeypotflutter/modules/account/page/account_signed_fail/view.dart';
import 'package:honeypotflutter/modules/account/page/account_signed_success/view.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/publicBussine/bussine_colors.dart';
import 'package:honeypotflutter/publicBussine/bussine_font.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_button.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_card.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_item.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/public_total.dart';
import 'logic.dart';

class AccountSignedPage extends StatefulWidget {
  const AccountSignedPage({Key? key}) : super(key: key);

  @override
  State<AccountSignedPage> createState() => _AccountSignedPageState();
}

/*
* 事件按钮
* */
clickAction() async {
  HLTotal.progress(status: "签约信息提交中...");
  try {
    await AccountApi.signInfo();
    HLGet.off(AccountSignedSuccessPage());
  } catch (error) {
    HLGet.to(AccountSignedFailPage());
  } finally {
    HLTotal.dismiss();
  }
}

class _AccountSignedPageState extends State<AccountSignedPage> {
  final AccountSignedLogic logic = Get.put(AccountSignedLogic());

  FocusNode _bankNode = FocusNode();
  FocusNode _phoneNode = FocusNode();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HLAppBar(navigationTitle: "签约"),
      body: MGCard(
        margin: EdgeInsets.all(12),
        nodeList: [_bankNode, _phoneNode],
        children: [
          MGItems(
            leadTitle: "姓名",
            trailingTitle: logic.userInfoLogic.userInfo.realName,
            showBottomSplit: true,
          ),
          MGItems(
            leadTitle: "身份证号",
            trailingTitle: logic.userInfoLogic.userInfo.idCardNumber,
            showBottomSplit: true,
          ),
          MGItems(
            leadTitle: "银行卡号",
            showBottomSplit: true,
            trailingWidget: _createTextField(
              hintText: "请输入银行卡号",
              controller: logic.bankController,
              node: _bankNode,
            ),
          ),
          MGItems(
            leadTitle: "手机号",
            trailingWidget: _createTextField(
              hintText: "请输入手机号码",
              controller: logic.phoneController,
              node: _phoneNode,
            ),
          )
        ],
      ),
      bottomNavigationBar: MGButton(
        title: "提交",
        padding: EdgeInsets.only(left: 30, right: 30, bottom: 30, top: 15),
        buttonAction: clickAction,
      ),
    );
  }

  /*
  * 创建输入框
  * */
  Widget _createTextField({String? hintText, TextEditingController? controller, FocusNode? node}) {
    TextField field = TextField(
      controller: controller,
      textAlign: TextAlign.end,
      keyboardType: TextInputType.number,
      focusNode: node,
      decoration: InputDecoration(
        hintText: hintText,
        hintStyle: TextStyle(fontSize: MGFont.textFont2, color: MGColors.text3Color),
        enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
        focusedBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.transparent)),
      ),
    );
    return Expanded(child: field);
  }

  @override
  void dispose() {
    Get.delete<AccountSignedLogic>();
    super.dispose();
  }
}
